package com.ruoyi.system.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ConventionMapper;
import com.ruoyi.system.domain.Convention;
import com.ruoyi.system.service.IConventionService;

import javax.annotation.Resource;
import javax.xml.crypto.Data;

/**
 * 挂号预约Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-26
 */
@Service
public class ConventionServiceImpl implements IConventionService 
{
    @Resource
    private ConventionMapper conventionMapper;

    /**
     * 查询挂号预约
     * 
     * @param id 挂号预约主键
     * @return 挂号预约
     */
    @Override
    public Convention selectConventionById(Long id)
    {
        return conventionMapper.selectConventionById(id);
    }

    /**
     * 查询挂号预约列表
     * 
     * @param convention 挂号预约
     * @return 挂号预约
     */
    @Override
    public List<Convention> selectConventionList(Convention convention)
    {
        List<Convention> list = conventionMapper.selectConventionList(convention);
        LocalDate date = LocalDate.now();
        LocalTime time = LocalTime.now();
        for (Convention co : list) {
           if (co.getState()!=2){
               LocalDate toLocalDate = co.getDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
               int compare = date.compareTo(toLocalDate);
               if (compare>0){//当前日期大于预约日期,过期
                   co.setState(1L);
               }else if (compare==0){ //日期相等，比较时间
                   int i = time.compareTo(co.getEndTime());
                   if (i>0){
                       co.setState(1L);
                   }
               }
           }
        }
        return list;
    }

    /**
     * 新增挂号预约
     * 
     * @param convention 挂号预约
     * @return 结果
     */
    @Override
    public int insertConvention(Convention convention)
    {
        convention.setCreateTime(DateUtils.getNowDate());
        return conventionMapper.insertConvention(convention);
    }

    /**
     * 修改挂号预约
     * 
     * @param convention 挂号预约
     * @return 结果
     */
    @Override
    public int updateConvention(Convention convention)
    {
        convention.setUpdateTime(DateUtils.getNowDate());
        return conventionMapper.updateConvention(convention);
    }

    /**
     * 批量删除挂号预约
     * 
     * @param ids 需要删除的挂号预约主键
     * @return 结果
     */
    @Override
    public int deleteConventionByIds(Long[] ids)
    {
        return conventionMapper.deleteConventionByIds(ids);
    }

    /**
     * 删除挂号预约信息
     * 
     * @param id 挂号预约主键
     * @return 结果
     */
    @Override
    public int deleteConventionById(Long id)
    {
        return conventionMapper.deleteConventionById(id);
    }

    public static void main(String[] args) {
//        LocalDate date1 = LocalDate.now();
//        LocalDate date2 = LocalDate.of(2024, 3, 4);
//        int comparison = date1.compareTo(date2);
//        Date date = new Date();
//        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
//        System.out.println(date);
//        System.out.println(localDate);
    }
}
